package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Poll;

public class GenericPollDAO extends AutoKeys implements org.jetnuke.dao.PollDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(GenericPollDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollModel#retrieveByPK(int)
	 */
	public Poll retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("PollModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Poll s = new Poll();

		if (rs.next()) {
			fillPollFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillPollFromResultSet(Poll s, ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillPollFromResultSet(Poll, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setARTID(rs.getInt("ARTID"));
		s.setPLANGUAGE(rs.getString("PLANGUAGE"));
		s.setPOLLID(rs.getInt("POLLID"));
		s.setPOLLTITLE(rs.getString("POLLTITLE"));
		s.setTIMESTAMP(rs.getTimestamp("TIMESTAMP"));
		s.setVOTERS(rs.getInt("VOTERS"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillPollFromResultSet(Poll, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollModel#getLast()
	 */
	public Poll getLast() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getLast() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PollModel.selectAllOrderTimestamp"));
		ResultSet rs = p.executeQuery();
		Poll poll = null;
		if (rs.next()) {
			poll = new Poll();
			fillPollFromResultSet(poll, rs);
		}
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getLast() - end"); //$NON-NLS-1$
		}
		return poll;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollModel#addNew(org.jetnuke.entities.Poll)
	 */
	public int addNew(Poll poll) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Poll) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this.getStatementForAutoKeys("PollModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PollModel.lastGeneratedPollID"));
		initNewPoll(poll, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Poll) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param poll
	 * @param p
	 */
	protected void initNewPoll(Poll poll, PreparedStatement p) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewPoll(Poll, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, poll.getARTID());
		p.setString(2, poll.getPLANGUAGE());
		p.setString(3, poll.getPOLLTITLE());
		p.setTimestamp(4, new Timestamp(poll.getTIMESTAMP().getTime()));
		p.setInt(5, poll.getVOTERS());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewPoll(Poll, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollModel#update(org.jetnuke.entities.Poll)
	 */
	public void update(Poll poll) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Poll) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("PollModel.update"));

		p.setInt(1, poll.getARTID());
		p.setString(2, poll.getPLANGUAGE());
		p.setString(3, poll.getPOLLTITLE());
		p.setTimestamp(4, new Timestamp(poll.getTIMESTAMP().getTime()));
		p.setInt(5, poll.getVOTERS());
		p.setInt(6, poll.getPOLLID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Poll) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollModel#selectAllOrderTimestamp()
	 */
	public List selectAllOrderTimestamp() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTimestamp() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PollModel.selectAllOrderTimestamp"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTimestamp() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Poll u = new Poll();
			fillPollFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollModel#selectAllNotArtIDOrderTimestampDesc()
	 */
	public List selectAllNotArtIDOrderTimestampDesc() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllNotArtIDOrderTimestampDesc() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PollModel.selectAllNotArtIDOrderTimestampDesc"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllNotArtIDOrderTimestampDesc() - end"); //$NON-NLS-1$
		}
		return list;
	}

}
